package com.itheima.service.db.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.PmsProductCategory;
import com.itheima.mapper.PmsProductCategoryMapper;
import com.itheima.service.db.PmsProductCategoryService;
import com.itheima.vo.PmsProductCategoryVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class PmsProductCategoryServiceImpl implements PmsProductCategoryService {

    @Autowired
    PmsProductCategoryMapper pmsProductCategoryMapper;

    @Override
    public List<PmsProductCategoryVo> findAll() {
        LambdaQueryWrapper<PmsProductCategory> qw = new LambdaQueryWrapper<>();

        List<PmsProductCategory> pmsProductCategories = pmsProductCategoryMapper.selectList(qw);

        List<PmsProductCategoryVo> voList = pmsProductCategories.stream().map(pmsProductCategory -> {
            qw.eq(PmsProductCategory::getParentId, pmsProductCategory.getId());
            List<PmsProductCategory> selectList = pmsProductCategoryMapper.selectList(qw);

            PmsProductCategoryVo vo = new PmsProductCategoryVo();
            BeanUtil.copyProperties(pmsProductCategory, vo);
            vo.setChildren(selectList);
            return vo;
        }).collect(Collectors.toList());

        return voList;
    }
}
